package pl.edu.pw.perg.compinfodb.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTree;

import org.springframework.richclient.application.PageComponentContext;
import org.springframework.richclient.application.support.AbstractView;
import org.springframework.richclient.security.ApplicationSecurityManager;

import pl.edu.pw.perg.compinfodb.command.AddEventCommandExecutor;
import pl.edu.pw.perg.compinfodb.command.AddNewCommandExecutor;
import pl.edu.pw.perg.compinfodb.command.SaveModelCommandExecutor;
import pl.edu.pw.perg.compinfodb.datamodel.CableDetailsTableModel;
import pl.edu.pw.perg.compinfodb.datamodel.HistoryTableModel;
import pl.edu.pw.perg.compinfodb.datamodel.TypeTreeModel;
import pl.edu.pw.perg.compinfodb.entities.User;
import pl.edu.pw.perg.compinfodb.manager.IElementTypeManager;

public class CablesManagerView extends AbstractView {

	private CableDetailsTableModel cableDetailsTableModel;
	private HistoryTableModel historyTableModel;
	private TypeTreeModel typeTreeModel;
	private CustomRendererTableFactory tableFactory = new CustomRendererTableFactory();
	private AddEventCommandExecutor addEventExecutor;
	private SaveModelCommandExecutor saveModelCommandExecutor;
	private AddNewCommandExecutor addNewCommandExecutor;
	private ApplicationSecurityManager applicationSecurityManager;
	@Override
	protected JComponent createControl() {

		JPanel view = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 0.5;
		c.weighty = 0.5;
		c.gridx = 0;
		c.gridy = 0;
		c.gridheight = 1;
		c.gridwidth = 1;
		
		JTable detailsTable = tableFactory.getElementTable(cableDetailsTableModel);
		JTable historyTable = tableFactory.getTable(historyTableModel);
		JTree cabelTypeTree = new JTree(typeTreeModel);
		cabelTypeTree.setRootVisible(false);
		cabelTypeTree.setShowsRootHandles(true);
		cabelTypeTree.addTreeSelectionListener(typeTreeModel);
		cabelTypeTree.setCellRenderer(new EquipmentTreeCellRenderer(getIconSource()));
		typeTreeModel.addElementTypeSelectionListener(cableDetailsTableModel);
		JScrollPane cableTreeScrPane = new JScrollPane(cabelTypeTree);
		JScrollPane detailsScrPane = new JScrollPane(detailsTable);
		JScrollPane historyScrPane = new JScrollPane(historyTable);
		cableDetailsTableModel.addPhysicalElementSelectionListener(historyTableModel);
		cableDetailsTableModel.addPhysicalElementSelectionListener(addEventExecutor);
		historyTable.addFocusListener(historyTableModel);
		detailsTable.addFocusListener(cableDetailsTableModel);
		
		historyTableModel.addActiveModelListener(saveModelCommandExecutor);
		historyTableModel.addActiveModelListener(addNewCommandExecutor);
		
		cableDetailsTableModel.addActiveModelListener(addNewCommandExecutor);
		cableDetailsTableModel.addActiveModelListener(saveModelCommandExecutor);
		cableDetailsTableModel.addModelModificationListener(saveModelCommandExecutor);
		
		cableDetailsTableModel.setEditable(applicationSecurityManager.isUserInRole(User.WRITE_ROLE));
		
		JSplitPane verticalSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, detailsScrPane, historyScrPane);
		JSplitPane horizontalSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, cableTreeScrPane, verticalSplitPane);

		verticalSplitPane.setDividerLocation(450);
		horizontalSplitPane.setDividerLocation(250);
		view.add(horizontalSplitPane, c);
		return view;
	}

	public void setCableDetailsTableModel(CableDetailsTableModel cableDetailsTableModel) {
		this.cableDetailsTableModel = cableDetailsTableModel;
	}

	public void setHistoryTableModel(HistoryTableModel historyTableModel) {
		this.historyTableModel = historyTableModel;
		addEventExecutor = new AddEventCommandExecutor(historyTableModel);
	
	}
	@Override
	protected void registerLocalCommandExecutors(PageComponentContext context) {
		context.register("addNewCommand", addNewCommandExecutor);
		context.register("saveModelCommand", saveModelCommandExecutor);
	}

	public void setTypeTreeModel(TypeTreeModel typeTreeModel) {
		this.typeTreeModel = typeTreeModel;
	}

	public void setElementTypeManager(IElementTypeManager elementTypeManager) {
		typeTreeModel = new TypeTreeModel(elementTypeManager, true);
	}

	public void setSaveModelCommandExecutor(
			SaveModelCommandExecutor saveModelCommandExecutor) {
		this.saveModelCommandExecutor = saveModelCommandExecutor;
	}

	public void setAddNewCommandExecutor(AddNewCommandExecutor addNewCommandExecutor) {
		this.addNewCommandExecutor = addNewCommandExecutor;
	}

	public void setApplicationSecurityManager(
			ApplicationSecurityManager applicationSecurityManager) {
		this.applicationSecurityManager = applicationSecurityManager;
	}
}
